﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Core.LockStep
{
    public interface IDataEnumerable<T> : IEnumerable<T> { }

    /// <summary>
    /// 迭代器是类结构，需要保存起来，避免GC
    /// </summary>
    public class DataEnumerable<T> : IDataEnumerable<T>, IEnumerator<T> where T : IStreamData
    {
        public T Current { get; private set; }

        object IEnumerator.Current { get { return Current; } }

        public DataContainer<T> container;

        private int index;

        public DataEnumerable(DataContainer<T> contaienr)
        {
            this.container = contaienr;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        public bool MoveNext()
        {
            if (index >= container.datas.Count)
                return false;

            Current = container.datas[index++];
            return true;
        }

        public void Reset()
        {
            Dispose();
        }

        public void Dispose()
        {
            index = 0;
        }
    }
}